package com.stox.client.workbench.ui.javafx.stage.titlebar;

import java.util.Collection;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;

import org.springframework.context.ApplicationContext;

import com.stox.client.core.application.Application;
import com.stox.client.ui.javafx.widget.ConfigurationPopupControl;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.client.workbench.ui.javafx.stage.StoxStage;
import com.stox.shared.i18n.MessageSource;
import com.stox.shared.util.StringUtil;

public class TitleBar extends ToolBar {

	private final StoxStage stoxStage;
	private ApplicationContext applicationContext;
	
	public TitleBar(StoxStage stoxStage, ApplicationContext applicationContext) {
		this.stoxStage = stoxStage;
		this.applicationContext = applicationContext;
		UI.style(this, CSS.titleBar);
		Region spacer1 = new Region();
		HBox.setHgrow(spacer1, Priority.ALWAYS);
		getItems().addAll(createStartButton(), spacer1,createWindowButtons());
	}
	
	private Node createStartButton(){
		ContextMenu contextMenu = new ContextMenu();
		Button startButton = new Button("Menu");
		UI.style(startButton, CSS.appsButton);
		
		startButton.setOnAction(new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				Bounds bounds = startButton.localToScreen(startButton.getLayoutBounds());
				contextMenu.show(startButton, bounds.getMaxX(), bounds.getMaxY());
			}
		});
		
		Menu applicationsMenu = new Menu(MessageSource.get("Applications"));
		MenuItem settingsMenuItem = new MenuItem("Settings");
		settingsMenuItem.setOnAction(new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				ConfigurationPopupControl control = new ConfigurationPopupControl(new Label("Hello"));
				control.show(StoxStage.getInstance());
			}
		});
		contextMenu.getItems().addAll(applicationsMenu, new MenuItem("Workspaces"), new MenuItem("Perspectives"), settingsMenuItem, new MenuItem("Help"));
		Collection<Application> applications = applicationContext.getBeansOfType(Application.class).values();
		for(Application application : applications){
			application.register();
			application.start();
			if(StringUtil.hasText(application.getName())){
				MenuItem applicationMenuItem = new MenuItem(application.getName());
				applicationMenuItem.setOnAction(new EventHandler<ActionEvent>(){
					@Override
					public void handle(ActionEvent event) {
						application.show();
					}
				});
				applicationsMenu.getItems().add(applicationMenuItem);
			}
		}
		
		return startButton;
	}

	private Node createWindowButtons(){
		Button minimizeButton = UI.mediumMinButton(MessageSource.get("Minimize"));
		minimizeButton.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				stoxStage.setIconified(true);
			}
		});
		
		Button maximizeButton = UI.mediumMaxButton(MessageSource.get("MaximizeOrRestore"));
		maximizeButton.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				stoxStage.getStageResizeRelocateMouseEventHandler().toggleMaximized();
			}
		});
		
		Button closeButton = UI.mediumCrossButton(MessageSource.get("Close"));
		closeButton.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				stoxStage.exit();
			}
		});
		
		return new HBox(minimizeButton, maximizeButton, closeButton);
	}
	
}

